/**
 * 
 */
package info.niwota.group;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;

/**
 * @author qiangli
 *
 */
public final class PlaylistUtils {

	private PlaylistUtils() {
	}

	private static Cursor query(Context context, Uri uri, String[] projection,
			String selection, String[] selectionArgs, String sortOrder) {
		try {
			ContentResolver resolver = context.getContentResolver();
			if (resolver == null) {
				return null;
			}
			return resolver.query(uri, projection, selection, selectionArgs,
					sortOrder);
		} catch (UnsupportedOperationException ex) {
			return null;
		}
	}


//	/*
//	 * Obtain the id for the default play list from the audio_playlists table.
//	 */
//	private long getPlaylistId(Resources res, String name) {
//		Uri uri = MediaStore.Audio.Playlists.getContentUri("external");
//		final String[] ids = new String[] { MediaStore.Audio.Playlists._ID };
//		final String where = MediaStore.Audio.Playlists.NAME + "=?";
//		final String[] args = new String[] { res.getString(
//				R.string.audio_db_playlist_name, name) };
//		Cursor cursor = query(uri, ids, where, args, null);
//		if (cursor == null) {
//			Log.v(TAG, "query returns null");
//		}
//		long id = -1;
//		if (cursor != null) {
//			cursor.moveToFirst();
//			if (!cursor.isAfterLast()) {
//				id = cursor.getLong(0);
//			}
//		}
//		cursor.close();
//		return id;
//	}
	
	public static long getPlaylistId(Context ctx, String email) {
		//Resources res = context.getResources();
		Uri uri = MediaStore.Audio.Playlists.getContentUri("external");
		final String[] ids = new String[] { MediaStore.Audio.Playlists._ID };
		final String where = MediaStore.Audio.Playlists.NAME + "=?";
		final String[] args = new String[] { ctx.getString(
				R.string.audio_db_playlist_name, email) };
		Cursor cursor = query(ctx, uri, ids, where, args, null);
		long id = -1;
		if (cursor != null) {
			cursor.moveToFirst();
			if (!cursor.isAfterLast()) {
				id = cursor.getLong(0);
			}
			cursor.close();
		}
		return id;
	}
}
